package com.haoyeteng.sxbd.modular.business.controller;

import com.baomidou.mybatisplus.mapper.EntityWrapper;
import com.haoyeteng.sxbd.core.base.controller.BaseController;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.PathVariable;
import org.apache.velocity.util.ArrayListWrapper;
import org.springframework.beans.factory.annotation.Autowired;
import com.haoyeteng.sxbd.core.base.model.ResponseResult;
import com.haoyeteng.sxbd.core.common.annotion.Permission;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import com.haoyeteng.sxbd.core.log.LogObjectHolder;
import com.haoyeteng.sxbd.core.util.Convert;
import com.haoyeteng.sxbd.core.util.ToolUtil;

import org.springframework.web.bind.annotation.RequestParam;
import com.haoyeteng.sxbd.modular.business.model.MsgConfig;
import com.haoyeteng.sxbd.modular.business.service.IMsgConfigService;
import com.haoyeteng.sxbd.modular.business.warpper.MsgConfigWarpper;
import com.haoyeteng.sxbd.modular.business.warpper.UserWarpper;
import com.haoyeteng.sxbd.modular.system.service.IRoleService;
import com.haoyeteng.sxbd.modular.system.warpper.RoleWarpper;

/**
 * 消息配置控制器
 *
 * @author haoyeteng
 * @Date 2019-06-12 10:41:43
 */
@Controller
@RequestMapping("/msgConfig")
public class MsgConfigController extends BaseController {

    private String PREFIX = "/business/msgConfig/";

    @Autowired
    private IMsgConfigService msgConfigService;
    
    @Autowired
    private IRoleService roleService;

    /**
     * 跳转到管理员消息配置页面
     */
    @RequestMapping("/admin")
    public String msgConfigAdmin(Model model) {
    	List<Map<String,Object>> list = new ArrayList<>();
    	List<Integer> msgTypeList = msgConfigService.selectMsgType(1);
    	for (Integer msgType : msgTypeList) {
    		Map<String,Object> map = new HashMap<>();
        	map.put("msgType", msgType);
        	List<Map<String,Object>> msgConfigList = msgConfigService.selectMaps(new EntityWrapper<MsgConfig>().eq("msg_category", 1).eq("msg_type", msgType));
        	map.put("msgConfigList", new MsgConfigWarpper(msgConfigList).warp());
        	list.add(map);
		}
    	model.addAttribute("list", list);
    	return PREFIX + "msgConfigAdmin.html";
    }

    /**
     * 跳转到消息接收角色配置页面
     */
    @RequestMapping("/admin/receive/{msgConfigId}")
    public String receive(@PathVariable Integer msgConfigId, Model model) {
      /*  MsgConfig msgConfig = msgConfigService.selectById(msgConfigId);
        model.addAttribute("item",msgConfig);
        LogObjectHolder.me().set(msgConfig);*/
    	model.addAttribute("msgConfigId", msgConfigId);
    	MsgConfig msgConfig = msgConfigService.selectById(msgConfigId);
    	Integer[] roleIds = Convert.toIntArray(msgConfig.getRoleIds());
    	//List<String> roleIdList = Arrays.asList(roleIds);
    	model.addAttribute("roleIds", roleIds);
    	List<Map<String, Object>> roleList = this.roleService.selectRoles(super.getPara("roleName"));
    	model.addAttribute("roleList", roleList);
        return PREFIX + "msgConfigAdmin_receive.html";
    }

    /**
     * 修改消息接收角色
     */
    @RequestMapping(value = "/admin/receive")
    @ResponseBody
    @Permission
    public Object receive(MsgConfig msgConfig) {
    	System.out.println("============"+msgConfig.getRoleIds());
    	System.out.println("============"+msgConfig.getMsgConfigId());
        msgConfigService.updateById(msgConfig);
        return SUCCESS_TIP;
    }

    
    /**
     * 管理员消息配置
     * @param msgConfig
     * @return
     */
    @RequestMapping(value = "/admin/set")
    @ResponseBody
    @Permission
    public Object adminSet(MsgConfig msgConfig) {
        msgConfigService.updateById(msgConfig);
        return SUCCESS_TIP;
    }
    
    
    /**
     * 跳转到用户消息配置页面
     */
    @RequestMapping("/user")
    public String msgConfigUser(Model model) {
    	List<Map<String,Object>> list = new ArrayList<>();
    	List<Integer> msgTypeList = msgConfigService.selectMsgType(1);
    	for (Integer msgType : msgTypeList) {
    		Map<String,Object> map = new HashMap<>();
        	map.put("msgType", msgType);
        	List<Map<String,Object>> msgConfigList = msgConfigService.selectMaps(new EntityWrapper<MsgConfig>().eq("msg_category", 0).eq("msg_type", msgType));
        	map.put("msgConfigList", new MsgConfigWarpper(msgConfigList).warp());
        	list.add(map);
		}
    	model.addAttribute("list", list);
    	return PREFIX + "msgConfigUser.html";
    }
    
    
    /**
     * 用户消息配置
     * @param msgConfig
     * @return
     */
    @RequestMapping(value = "/user/set")
    @ResponseBody
    @Permission
    public Object usSet(MsgConfig msgConfig) {
        msgConfigService.updateById(msgConfig);
        return SUCCESS_TIP;
    }
}
